<template>
  <div style="padding: 20px">
    <div class="title_font">
      产品管理
    </div>
  </div>
  <ContentWrap>

    <!-- 搜索工作栏 -->
    <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
    >
      <el-form-item>
        <el-button @click="addproduct"><Icon icon="ep:search" class="mr-5px" /> 增加产品</el-button>
        <el-button @click=""><Icon icon="ep:refresh" class="mr-5px" /> 批量删除</el-button>
      </el-form-item>
      <el-form-item label="产品名称：" prop="name" >
        <el-input
            v-model="queryParams.productName"
            placeholder="请输入产品名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品类型：" prop="name" >
        <el-select v-model="blicityList" clearable placeholder="请选择状态">
          <el-option
            v-for="dict in blicityList"
            :key="dict.id"
            :label="dict.typeNames"
            :value="dict.id"
          />
          </el-select>
      </el-form-item>

      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
            v-model="queryParams.createTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>

    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="产品编号" align="center" prop="id" width="150px" />
      <el-table-column label="ID" align="center" prop="createTime" width="100px" />
      <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          width="150px"
      />
      <el-table-column label="图标" align="center"  width="150px" >
        <template #default="scope">
          <img :src="scope.row.iconStr" style="width: 15px;height: 15px">
        </template>
      </el-table-column>

      <el-table-column label="产品名称" align="center" prop="productName" width="150px" />
      <el-table-column label="最高贷款额度" align="center" prop="maxAmount" width="140px" />
      <el-table-column label="年利息（%）" align="center" prop="annualInterest" width="150px" >
      </el-table-column>
      <el-table-column label="贷款期限（个月）" align="center" prop="loanTime" width="140px" >

      </el-table-column>
      <el-table-column label="产品类型" align="center" prop="productTypeName" width="150px" >

      </el-table-column>
      <el-table-column label="操作" align="center" width="200px">
        <template #default="scope">
          <el-button
              link
              type="primary"
              @click="openForm('look', scope.row.id)"
              checkPermi="['client:users:look']"
          >
            查看
          </el-button>
          <el-button
              link
              type="primary"
              @click="openForm('look', scope.row.id)"
              checkPermi="['client:product:look']"
          >
            编辑
          </el-button>
          <el-button
              link
              type="primary"
              @click="handleDelete(scope.row.id)"
              checkPermi="['system:users:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </ContentWrap>

</template>

<script setup lang="ts">
import { ref } from 'vue';
import { dateFormatter } from '@/utils/formatTime'
import * as ProductApi from '@/api/hutai/productMana'
import * as UsersApi from "@/api/hutai/certificateHolder";
const { push } = useRouter()

const { t } = useI18n() // 国际化
defineOptions({ name: 'HutaiClientpoolPool' })

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const addproduct=()=>{
    push("addProduct")
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await UsersApi.deleteUsers(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}


const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  id:null,
  productName: null,
  iconStr: null,
  maxAmount: null,
  annualInterest: null,
  loanTime: null,
  productType: null,
  productLable: null,
  productIntroduce: null,
  productImg: null,
  status: null,
  createTime: null,
})
const queryFormRef = ref() // 搜索的表单
const blicityList=reactive([])
const  getblicityType = ProductApi.getblicityType()
getblicityType.then(res=>{
    for (const item in res){
        console.log(item)
        blicityList.push({id:res[item].id,name:res[item].typeNames})
    }
})




/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProductApi.getProductList(queryParams)
      console.log(data)
      list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const openForm = (type: string, id?: number) => {
  push('productDetails?id='+id)
}


/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style>
.title_font{
  margin-bottom: 10px;
  font-size: 16px;
  font-weight: 600;
}
</style>
